//
//  FZTabBar.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/19/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit


//MARK: 第一次使用代理的方法实现点击通知控制器我点击了按钮
//NSObjectProtocol 不继承这个无法使用weak 来修饰代理属性
//@objc protocol FZTabBarDelegate:NSObjectProtocol {
//   optional func composeButtonClick()
//}

class FZTabBar: UITabBar {
//=======================属性=============================
    //代理属性
//    weak var fzDelegate: FZTabBarDelegate?
    var composeButtonClosure: (()->())?
    
//=======================初始化方法=======================
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 添加监听方法: 加上private修饰后，必须加上@objc
   @objc private func clickComposeButton() {
        //?代替了respond
//        fzDelegate?.composeButtonClick?()
    
        //执行闭包
        composeButtonClosure!()
    }
    
//===================布局子控件===============================
    //MARK: 重写layoutSubviews
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        var index: Int = 0
        let width: CGFloat = self.frame.width * 0.2
        composeButton.frame.size.width = width
        //重新布局tabBar上的UITabBarButton的frame
        
        for item in subviews {
            
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                item.frame.origin.x = CGFloat(index) * width
                item.frame.size.width = width
                index += 1
                if index == 2 {
                    composeButton.frame.origin.x = CGFloat(index) * width
                    composeButton.frame.origin.y = 0
                    composeButton.frame.size = CGSizeMake(width, self.frame.size.height)
                    index += 1
                }
            }
        }
    }
    //MARK: 懒加载空间
    private lazy var composeButton: UIButton = {
        () -> UIButton in
        
        let btn: UIButton = UIButton(imageNamed: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button", target: self, action: #selector(clickComposeButton))
        
        
        return btn
    }()
    
}
